package weblicitacao

import org.springframework.dao.DataIntegrityViolationException

class EmpenhoController {

	static navigation = [
		group:'tabs',
		order:3,
		title:'Empenho',
		action:'index'
	]

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index() {
		redirect(action: "list", params: params)
	}

	def list() {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[empenhoInstanceList: Empenho.list(params), empenhoInstanceTotal: Empenho.count()]
	}

	def create() {
		[empenhoInstance: new Empenho(params)]
	}

	def save() {
		def empenhoInstance = new Empenho(params)
		if (!empenhoInstance.save(flush: true)) {
			render(view: "create", model: [empenhoInstance: empenhoInstance])
			return
		}

		flash.message = message(code: 'default.created.message', args: [message(code: 'empenho.label', default: 'Empenho'), empenhoInstance.id])
		redirect(action: "show", id: empenhoInstance.id)
	}

	def show() {
		def empenhoInstance = Empenho.get(params.id)
		if (!empenhoInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'empenho.label', default: 'Empenho'), params.id])
			redirect(action: "list")
			return
		}

		def licitacao = empenhoInstance.licitacao

		empenhoInstance.licitacao.itens.each {
			itensInstance.add(it)
		}
		
		empenhoInstance.licitacao.lotes.each {
			lotesInstance.add(it)
		}

		def itensEmpenhoInstance = empenhoInstance.itensEmpenho

		def lotesEmpenhoInstance = empenhoInstance.itensEmpenho

		[empenhoInstance: empenhoInstance, itensInstance: itensInstance, itensEmpenhoInstance:itensEmpenhoInstance,
			lotesInstance: lotesInstance, lotesEmpenhoInstance:lotesEmpenhoInstance]
	}

	def edit() {
		def empenhoInstance = Empenho.get(params.id)
		if (!empenhoInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'empenho.label', default: 'Empenho'), params.id])
			redirect(action: "list")
			return
		}

		[empenhoInstance: empenhoInstance]
	}

	def update() {
		def empenhoInstance = Empenho.get(params.id)
		if (!empenhoInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'empenho.label', default: 'Empenho'), params.id])
			redirect(action: "list")
			return
		}

		if (params.version) {
			def version = params.version.toLong()
			if (empenhoInstance.version > version) {
				empenhoInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
				[message(code: 'empenho.label', default: 'Empenho')] as Object[],
				"Another user has updated this Empenho while you were editing")
				render(view: "edit", model: [empenhoInstance: empenhoInstance])
				return
			}
		}

		empenhoInstance.properties = params

		if (!empenhoInstance.save(flush: true)) {
			render(view: "edit", model: [empenhoInstance: empenhoInstance])
			return
		}

		flash.message = message(code: 'default.updated.message', args: [message(code: 'empenho.label', default: 'Empenho'), empenhoInstance.id])
		redirect(action: "show", id: empenhoInstance.id)
	}

	def delete() {
		def empenhoInstance = Empenho.get(params.id)
		if (!empenhoInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'empenho.label', default: 'Empenho'), params.id])
			redirect(action: "list")
			return
		}

		try {
			empenhoInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'empenho.label', default: 'Empenho'), params.id])
			redirect(action: "list")
		}
		catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'empenho.label', default: 'Empenho'), params.id])
			redirect(action: "show", id: params.id)
		}
	}

	def addItemEmpenho() {
		def itemInstance = Item.get(params.idItem)
		def empenho = Empenho.get(params.id)
		
		empenho.addToItensEmpenho(new ItemEmpenho('item',itemInstance))
	}
	
	def delItemEmpenho() {
		def empenho = Empenho.get(params.id)
		def itemEmpenho = ItemEmpenho.get(params.idItem)
		
		itemEmpenho.delete()
	}
	
	def addLoteEmpenho() {
		def loteInstance = Lote.get(params.idLote)
		def empenho = Empenho.get(params.id)
		
		empenho.addToItensEmpenho(new ItemEmpenho('lote',loteInstance))
	}
	
	def delLoteEmpenho() {
		def empenho = Empenho.get(params.id)
		def itemEmpenho = ItemEmpenho.get(params.idLote)
		
		itemEmpenho.delete()
	}
}
